<template>
  <div class="app-container">
    <!--搜索区-->
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="handleQuery()" size="small">
          查询搜索
        </el-button>
        <el-button style="float:right;margin-right: 15px" @click="resetQuery()" size="small">
          重置
        </el-button>
        <div style="margin-top: 15px">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="140px">
            <el-form-item label="认证状态" prop="state">
              <el-select v-model="queryParams.state" placeholder="请选择" clearable >
                <el-option label="待处理" :value="1" />
                <el-option label="通过" :value="2" />
                <el-option label="拒绝" :value="3" />
              </el-select>
            </el-form-item>
            <el-form-item label="申请时间">
              <el-date-picker
                v-model="dateRange"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
              ></el-date-picker>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>

    <!--操作按钮区-->
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
    </el-card>

    <!--数据列表区-->
    <div class="table-container">
      <el-table v-loading="loading" :data="becomeList" border>
        <el-table-column prop="name" label="真实姓名" width="150"/>
        <el-table-column label="认证级别" align="center" prop="level" width="150">
          <template slot-scope="scope">
            {{ scope.row.level | setLevelName }}
          </template>
        </el-table-column>
        <el-table-column prop="idnum" label="身份证号" width="270"/>
        <el-table-column prop="phone" label="手机号" width="150"/>
        <el-table-column prop="userNickname" label="申请用户名" width="200"/>
        <el-table-column prop="mobile" label="用户手机" width="150"/>
        <el-table-column label="状态" align="center" prop="state" width="150">
          <template slot-scope="scope">
            {{ scope.row.state | setStateName }}
          </template>
        </el-table-column>
        <el-table-column  label="申请时间" width="150">
          <template slot-scope="scope">
            <span>{{ dateTimeFormat(scope.row.becomeDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['auth:become:edit']"
              v-if="scope.row.state != 1"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['auth:become:edit']"
              v-if="scope.row.state == 1"
            >审核</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['auth:become:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页区-->
    <div class="pagination-container">
      <el-pagination
        layout="total, sizes,prev, pager, next,jumper"
        :current-page="queryParams.pageNum"
        :page-size="queryParams.pageSize"
        :page-sizes="[10,15,20]"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 添加或修改房源表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <div class="form-container-border">
          <el-row>
            <el-col :span="6" class="table-cell-title">真实姓名</el-col>
            <el-col class="table-cell" :span="18">{{form.name}}</el-col>
          </el-row>
          <el-row>
            <el-col :span="6" class="table-cell-title">认证级别</el-col>
            <el-col class="table-cell" :span="18">{{form.level | setLevelName}}</el-col>
          </el-row>
          <el-row>
            <el-col :span="6" class="table-cell-title">身份证号</el-col>
            <el-col class="table-cell" :span="18">{{form.idnum }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="6" class="table-cell-title">手机号</el-col>
            <el-col class="table-cell" :span="18">{{form.phone }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="6" class="table-cell-title" style="height: 70px">身份证照片</el-col>
            <el-col class="table-cell" :span="18" style="height: 70px">
              <span v-for="item in form.imgUrl" style="margin-left: 10px">
                <img style="width:50px;height:50px" :src="item" @click="handlePreview(item)">
              </span>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6" class="table-cell-title">申请人姓名</el-col>
            <el-col class="table-cell" :span="18">{{form.userNickname }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="6" class="table-cell-title">用户手机</el-col>
            <el-col class="table-cell" :span="18">{{form.mobile }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="6" class="table-cell-title">申请时间</el-col>
            <el-col class="table-cell" :span="18">{{ dateTimeFormat(form.becomeDate) }}</el-col>
          </el-row>
          <el-row>
            <el-col :span="6" class="table-cell-title" style="height: auto">申请明细</el-col>
            <el-col class="table-cell" :span="18" style="height: auto">
              <el-table border class="standard-margin" :data="houseList">
                <el-table-column label="房源名称" width="160" align="center">
                  <template slot-scope="scope">{{scope.row.houseName}}</template>
                </el-table-column>
                <el-table-column label="地址" align="center">
                  <template slot-scope="scope">{{scope.row.houseAdr}}</template>
                </el-table-column>
                <el-table-column label="地图地址" align="center">
                  <template slot-scope="scope">{{scope.row.mapAdr}}</template>
                </el-table-column>
                <el-table-column label="图片" width="180" align="center">
                  <template slot-scope="scope">
                    <img v-for="item in scope.row.imgUrl" style="width:50px;height:50px;margin-left: 10px" :src="item" @click="handlePreview(item)">
                  </template>
                </el-table-column>
                <el-table-column label="其他说明" width="180" align="center">
                  <template slot-scope="scope">{{scope.row.other}}</template>
                </el-table-column>
              </el-table>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6" class="table-cell-title" style="height: 60px">状态</el-col>
            <el-col class="table-cell" :span="18" style="height: 60px">
              <span class="font-small">
                <el-radio-group v-model="form.state">
                <el-radio :label="1">待处理</el-radio>
                <el-radio :label="2">通过</el-radio>
                <el-radio :label="3">拒绝</el-radio>
              </el-radio-group></span><br>
              <span class="font-small">认证"通过"后，将获得所设置的对应认证级别的发贴量</span>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="6" class="table-cell-title" style="height: 60px">审核原因</el-col>
            <el-col class="table-cell" :span="18" style="height: 60px">
              <el-input placeholder="请输入审核原因" v-model="form.result" v-if="!isView"></el-input>
              <span class="font-small" v-if="isView">{{form.result}}</span>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="!isView">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible" width="600px">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
  import { listBecome, getBecomeInfo, delBecome, updateBecome } from "@/api/become/become";

  export default {
    name: "Become",
    filters: {
      setLevelName(val) {
        let text = '普通'
        switch (val) {
          case 2:
            text = '房东'
            break
          case 3:
            text = '房管员'
            break
        }
        return text
      },
      setStateName(val) {
        let text = '待处理'
        switch (val) {
          case 2:
            text = '通过'
            break
          case 3:
            text = '拒绝'
            break
        }
        return text
      }
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        becomeList: [],
        houseList: [],
        // 日期范围
        dateRange: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 图片预览
        dialogVisible: false,
        // 图片预览地址
        dialogImageUrl:null,
        // 是否查看
        isView: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询房源表列表 */
      getList() {
        this.loading = true;
        listBecome(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.becomeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {};
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 查看详情操作 */
      handleView(row) {
        this.isView = true;
        const becomeIds = row.becomeId || this.ids;
        getBecomeInfo(becomeIds).then(response => {
          this.form = response.data;
          this.houseList = response.data.house;
          this.open = true
          this.title = "详情";
        });
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.isView = false;
        const becomeIds = row.becomeId || this.ids;
        getBecomeInfo(becomeIds).then(response => {
          this.form = response.data;
          this.houseList = response.data.house;
          this.open = true
          this.title = "审核";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.becomeId != null) {
              updateBecome(this.form.becomeId,this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const becomeIds = row.becomeId || this.ids;
        this.$modal.confirm('是否确认删除编号为"' + becomeIds + '"的数据项？').then(function() {
          return delBecome(becomeIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      handleSizeChange(val) {
        this.queryParams.pageNum = 1;
        this.queryParams.pageSize = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.queryParams.pageNum = val;
        this.getList();
      },
      // 预览图片
      handlePreview(url) {
        this.dialogVisible = true;
        this.dialogImageUrl = url;
      },
    }
  };
</script>
<style scoped>
  .standard-margin {
    margin-top: 15px;
  }
  .form-container-border {
    border-left: 1px solid #DCDFE6;
    border-top: 1px solid #DCDFE6;
    height: 62vh;
    overflow-x: hidden;
  }
  .table-cell-title {
    line-height: 20px;
    border-right: 1px solid #DCDFE6;
    border-bottom: 1px solid #DCDFE6;
    padding: 10px;
    background: #F2F6FC;
    text-align: left;
    font-size: 14px;
    color: #303133;
  }
  .table-cell {
    height: 41px;
    line-height: 20px;
    border-right: 1px solid #DCDFE6;
    border-bottom: 1px solid #DCDFE6;
    padding: 10px;
    font-size: 14px;
    color: #606266;
    text-align: left;
    overflow: hidden;
  }
</style>
